const axios = require("axios");
axios.default.interceptors.request.use(
  config => {
    let url = config.url;
    // get参数进行百分比编码
    if (config.method === "get" && config.params) {
      url += "?";
      let keys = Object.keys(config.params);
      for (let key of keys) {
        url += `${key}=${encodeURIComponent(config.params[key])}&`;
      }
      url = url.substring(0, url.length - 1);
      config.params = {};
    }
    config.url = url;
    return config;
  },
  function(error) {
    return Promise.reject(error);
  }
);
const get = function(url, config) {
  return axios.get(url, config).then(response => {
    const { data, status, statusText } = response;
    if (status == 200 || status == 304) {
      return data;
    } else {
      throw new Error(statusText);
    }
  });
};

// 第一次获取正在热映的电影列表
export const getInitMovieList = function() {
  return get("/ajax/movieOnInfoList");
};
export const getMoreMovieList = function(movieIds) {
  return get("/ajax/moreComingList", {
    params: {
      token: "",
      movieIds: movieIds.join(",")
    }
  });
};

export const getCitis = function () {
  return get('/dianying/cities.json')
}
